#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "MLX90393.h"
#include "Serial.h"
// I2C从机地址定义（写操作）
#define SLAVE1_ADDR_WRITE    0x18  // (0x1A,0x18)
#define SLAVE2_ADDR_WRITE    0x1c  // (0x1E,0x1c)
#define SLAVE3_ADDR_WRITE    0x1A
#define SLAVE4_ADDR_WRITE    0x1E

int16_t Data_1, Data_2,Data_3,Data_4,Data_5,Data_6,Data_7,Data_8,Data_9,Data_10,Data_11,Data_12;

void Send_Magnetic_Data(int16_t s1x, int16_t s1y, int16_t s1z, int16_t s2x, int16_t s2y, int16_t s2z,
    int16_t s3x, int16_t s3y, int16_t s3z, int16_t s4x, int16_t s4y, int16_t s4z)
{   
    // 2. 创建字节数组（含帧头、数据、校验和、帧尾，总长度16字节）
    uint8_t sendArray[28];
    
    // 3. 设置帧头（固定值，用于帧同步）
    sendArray[0] = 0xAA;  // 帧头1
    sendArray[1] = 0x55;  // 帧头2
    
    // 4. 拆分数据为高字节和低字节，按顺序存入数组
    // S1传感器X轴
    sendArray[2] = (uint8_t)(s1x >> 8);  
    sendArray[3] = (uint8_t)(s1x & 0xFF);         
    // S1传感器Y轴
    sendArray[4] = (uint8_t)(s1y >> 8);
    sendArray[5] = (uint8_t)(s1y & 0xFF);
    // S1传感器Z轴
    sendArray[6] = (uint8_t)(s1z >> 8);
    sendArray[7] = (uint8_t)(s1z & 0xFF);
    // S2传感器X轴
    sendArray[8] = (uint8_t)(s2x >> 8);
    sendArray[9] = (uint8_t)(s2x & 0xFF);
    // S2传感器Y轴
    sendArray[10] = (uint8_t)(s2y >> 8);
    sendArray[11] = (uint8_t)(s2y & 0xFF);
    // S2传感器Z轴
    sendArray[12] = (uint8_t)(s2z >> 8);
    sendArray[13] = (uint8_t)(s2z & 0xFF);
    // S3传感器X轴
    sendArray[14] = (uint8_t)(s3x >> 8);  
    sendArray[15] = (uint8_t)(s3x & 0xFF);         
    // S3传感器Y轴
    sendArray[16] = (uint8_t)(s3y >> 8);
    sendArray[17] = (uint8_t)(s3y & 0xFF);
    // S3传感器Z轴
    sendArray[18] = (uint8_t)(s3z >> 8);
    sendArray[19] = (uint8_t)(s3z & 0xFF);
    // S4传感器X轴
    sendArray[20] = (uint8_t)(s4x >> 8);
    sendArray[21] = (uint8_t)(s4x & 0xFF);
    // S4传感器Y轴
    sendArray[22] = (uint8_t)(s4y >> 8);
    sendArray[23] = (uint8_t)(s4y & 0xFF);
    // S4传感器Z轴
    sendArray[24] = (uint8_t)(s4z >> 8);
    sendArray[25] = (uint8_t)(s4z & 0xFF);
    
    // 5. 计算校验和（示例：简单异或校验，可替换为累加和等）
    uint8_t checksum = 0;
    for (uint8_t i = 2; i <= 25; i++) {
        checksum ^= sendArray[i];  // 对数据部分进行异或校验
    }
    sendArray[26] = checksum;  // 将计算出的校验和存储到数组的指定位置
    
    // 6. 设置帧尾（用于标记帧结束）
    sendArray[27] = 0x0D;  // 帧尾（可根据需要修改为其他值）
    
    // 7. 通过串口发送完整帧
    Serial_SendArray(sendArray, 28);
}

int main(void)
{
    
	// 从机地址定义（7位地址左移1位后的写地址）
    uint8_t slave1Addr = SLAVE1_ADDR_WRITE;
    uint8_t slave2Addr = SLAVE2_ADDR_WRITE;
    uint8_t slave3Addr = SLAVE3_ADDR_WRITE;
    uint8_t slave4Addr = SLAVE4_ADDR_WRITE;

	//串口初始化
	Serial_Init();
    // 初始化两个传感器
    uint8_t status1 = MLX90393_Init(slave1Addr);
    Delay_ms(20);
    uint8_t status2 = MLX90393_Init(slave2Addr);
    Delay_ms(20);
    uint8_t status3 = MLX90393_Init(slave3Addr);
    Delay_ms(20);
    uint8_t status4 = MLX90393_Init(slave4Addr);
    
    while (1) 
	{
        MLX90393_GetData(slave1Addr,&Data_1, &Data_2, &Data_3);
        Delay_ms(20);
        MLX90393_GetData(slave2Addr,&Data_4, &Data_5, &Data_6);
        Delay_ms(20);  
        MLX90393_GetData(slave3Addr,&Data_7, &Data_8, &Data_9);
        Delay_ms(20); 
        MLX90393_GetData(slave4Addr,&Data_10, &Data_11, &Data_12);
        Delay_ms(20);  
        Send_Magnetic_Data(Data_1,Data_2,Data_3,Data_4,Data_5,Data_6,Data_7,Data_8,Data_9,Data_10,Data_11,Data_12);
        Delay_ms(100);  // 5Hz刷新率,
    }
    
}
